package servlets.invitation;

import hibernate.method.InvitationMethods;
import hibernate.method.RelationMethods;
import hibernate.model.Relation;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class InvitationSend
 */
@WebServlet("/InvitationSend")
public class InvitationSend extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InvitationSend() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
PrintWriter out = response.getWriter();
		
		int inviting = Integer.parseInt((String) request.getParameter("sender_id"));
		int invited = Integer.parseInt(request.getParameter("receiver"));
		if (invited == 0) {
			out.println("<script type=\"text/javascript\">alert(\"Nie podales uzytkownika\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");
			
		} else {
		
		System.out.println(invited + " " + inviting);
		
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		InvitationMethods inv_m = new InvitationMethods();
		RelationMethods rel_m = new RelationMethods();
		List<Relation> rel1 = rel_m.findRelationFromTo(sessionFactory, session, inviting, invited);
		List<Relation> rel2 = rel_m.findRelationToFrom(sessionFactory, session, invited, inviting);
		
		if(rel1.isEmpty() && rel2.isEmpty()){
			inv_m.sendInvitation(sessionFactory, session, inviting, invited);
			out.println("<script type=\"text/javascript\">alert(\"Zaproszenie zostalo juz wczesniej wyslane\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");
				
		}else 
			out.println("<script type=\"text/javascript\">alert(\"Juz jestescie znajomymi\"); window.location.href = \"social/index.jsp?page=friends&group=currentFriends\";</script>");
		
		try {
			session.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Wyslano zaproszenie do grona znajomych\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");
		
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Wysylanie zaproszenia do grona znajomych nie powiodlo sie. Za utrudnienia przepraszamy\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");
			
		}

		
		
	}
	}

}
